package com.shopping.www.controller;


import com.alibaba.fastjson.JSON;
import com.shopping.www.model.RegisterBean;
import com.shopping.www.model.User;
import com.shopping.www.service.UserService;
import com.shopping.www.util.CheckCodeUtil;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    private UserService service1=new UserService();
  public void loginServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
      String username=request.getParameter("username");
      username=new String(username.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
      String password=request.getParameter("password");
      password=new String(password.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
      String status=request.getParameter("status");
      status=new String(status.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
      User user=service1.login(username,password,status);
      if(user!=null) {
          if(user.getBanStatus()!=1) {
              response.setContentType("text/javascript;charset=utf8");
              String jsonString = JSON.toJSONString(user);
              response.getWriter().write(jsonString);
          }else{
              response.getWriter().write("hasBanned");
          }
      }
      else if("".equals(status))response.getWriter().write("noSelect");
      else if(username.length()>0&&password.length()>0)response.getWriter().write("noUser");
      else if(username.length()==0)response.getWriter().write("noInputUsername");
      else response.getWriter().write("noInputPassword");
  }
    public void checkCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        ServletOutputStream fos=response.getOutputStream();
        String checkCode= CheckCodeUtil.outputVerifyImage(100,50,fos,4);//系统生成的验证码
        //存入Session
        session.setAttribute("checkCodeGen",checkCode);
        System.out.println(checkCode);
    }
    public void registerServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setHeader("content-type","application:json;charset=utf8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods","POST");
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
        String checkCode=request.getParameter("code");//用户输入的验证码
        HttpSession session=request.getSession();
        String checkCodeGen=(String)session.getAttribute("checkCodeGen");//拿出系统的验证码
        //读取请求体内容
        BufferedReader br=request.getReader();
        String params=br.readLine();
        RegisterBean registerBean= JSON.parseObject(params, RegisterBean.class);//用户输入
        String status=request.getParameter("status");
        status=new String(status.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        if(registerBean.getUsername().length()==0){
            response.getWriter().write("noInputUsername");
            return;
        }else
        if(registerBean.getPassword().length()==0){
            response.getWriter().write("noInputPassword");
            return;
        }else
        if(checkCode.length()==0){
            response.getWriter().write("noInputCode");
            return;
        }
        else if(status.length()==0){
            response.getWriter().write("noInputStatus");
            return;
        }
        else if(!checkCodeGen.equalsIgnoreCase(checkCode)) {
            response.getWriter().write("codeErr");
            return;
        }
        boolean flag=service1.register(registerBean,status);
        if(flag){
            response.getWriter().write("success");
        }else{
            response.getWriter().write("fail");
        }
    }
}
